package com.wmeimob.fastboot.jiubei.service;

import com.wmeimob.fastboot.jiubei.entity.ShopOrderItems;
import com.wmeimob.fastboot.jiubei.entity.ShopOrders;
import com.wmeimob.fastboot.jiubei.entity.ShopGoodsStatistics;
import com.wmeimob.fastboot.jiubei.mapper.ShopGoodsStatisticsMapper;
import com.wmeimob.fastboot.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @ClassName ShopGoodsStatisticsServiceImpl
 * @Description 商品统计 服务类实现
 * @Author wjt
 * @Date Mon Sep 07 00:55:01 CST 2020
 **/

@Service
@Transactional(rollbackFor = {Exception.class})
@Slf4j
public class ShopGoodsStatisticsServiceImpl implements ShopGoodsStatisticsService {

    @Resource
    private ShopGoodsStatisticsMapper shopGoodsStatisticsMapper;


    @Override
    public List<ShopGoodsStatistics> list(ShopGoodsStatistics shopGoodsStatistics) {
        Example example = new Example(ShopGoodsStatistics.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(shopGoodsStatistics.getId())) {
            criteria.andEqualTo("id", shopGoodsStatistics.getId());
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getType())) {
            criteria.andLike("type", StringUtils.fullFuzzy(shopGoodsStatistics.getType()));
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getName())) {
            criteria.andLike("name", StringUtils.fullFuzzy(shopGoodsStatistics.getName()));
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getShopGoodsId())) {
            criteria.andEqualTo("shopGoodsId", shopGoodsStatistics.getShopGoodsId());
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getSpecification())) {
            criteria.andLike("specification", StringUtils.fullFuzzy(shopGoodsStatistics.getSpecification()));
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getUnit())) {
            criteria.andLike("unit", StringUtils.fullFuzzy(shopGoodsStatistics.getUnit()));
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getSalesVolume())) {
            criteria.andEqualTo("salesVolume", shopGoodsStatistics.getSalesVolume());
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getSaleNum())) {
            criteria.andEqualTo("saleNum", shopGoodsStatistics.getSaleNum());
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getDel())) {
            criteria.andEqualTo("del", shopGoodsStatistics.getDel());
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getGmtCreate())) {
            criteria.andEqualTo("gmtCreate", shopGoodsStatistics.getGmtCreate());
        }
        if (!StringUtils.isEmpty(shopGoodsStatistics.getGmtModified())) {
            criteria.andEqualTo("gmtModified", shopGoodsStatistics.getGmtModified());
        }
        criteria.andEqualTo("del", false);
        example.orderBy("id").desc();
        List<ShopGoodsStatistics> shopGoodsStatisticss = shopGoodsStatisticsMapper.selectByExample(example);
        return shopGoodsStatisticss;
    }

    @Override
    public ShopGoodsStatistics queryShopGoodsStatisticsById(Object id) {
        return shopGoodsStatisticsMapper.selectByPrimaryKey(id);
    }


    @Override
    public void addShopGoodsStatistics(ShopGoodsStatistics shopGoodsStatistics) {
        shopGoodsStatistics.setGmtCreate(new Date());
        shopGoodsStatisticsMapper.insertSelective(shopGoodsStatistics);
    }

    @Override
    public void removeShopGoodsStatistics(Object id) {
        ShopGoodsStatistics shopGoodsStatistics = new ShopGoodsStatistics();
        shopGoodsStatistics.setId(Integer.parseInt(id.toString()));
        shopGoodsStatistics.setDel(true);
        shopGoodsStatisticsMapper.updateByPrimaryKeySelective(shopGoodsStatistics);
    }

    @Override
    public void modifyShopGoodsStatistics(ShopGoodsStatistics shopGoodsStatistics) {
        shopGoodsStatistics.setGmtModified(new Date());
        shopGoodsStatisticsMapper.updateByPrimaryKeySelective(shopGoodsStatistics);
    }

    @Override
    public void addRecord(ShopOrders shopOrders) {
        List<ShopOrderItems> items = shopOrders.getItems();
        items.forEach(item -> {
            //查询当前记录是否存在
            ShopGoodsStatistics goodsStatistics = new ShopGoodsStatistics();
            goodsStatistics.setShopGoodsId(item.getGoodsId());
            Example example = new Example(ShopGoodsStatistics.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("shopGoodsId", item.getGoodsId());
            int count = shopGoodsStatisticsMapper.selectCountByExample(example);
            if (count <= 0) {
                ShopGoodsStatistics shopGoodsStatistics = new ShopGoodsStatistics();
                shopGoodsStatistics.setType(shopOrders.getOrderType());
                shopGoodsStatistics.setShopGoodsId(item.getGoodsId());
                shopGoodsStatistics.setName(item.getGoodsName());
                shopGoodsStatistics.setSpecification(item.getGoodsSkuName());
                shopGoodsStatistics.setSaleNum(item.getSaleQuantity());
                shopGoodsStatistics.setSalesVolume(item.getItemsPayAmount());
                shopGoodsStatistics.setGmtCreate(new Date());
                shopGoodsStatisticsMapper.insertSelective(shopGoodsStatistics);
            } else {
                ShopGoodsStatistics shopGoodsStatistics = new ShopGoodsStatistics();
                shopGoodsStatistics.setShopGoodsId(item.getGoodsId());
                shopGoodsStatistics.setSaleNum(item.getSaleQuantity());
                shopGoodsStatistics.setSalesVolume(item.getItemsPayAmount());
                shopGoodsStatisticsMapper.increaseSales(shopGoodsStatistics);
            }
        });

    }

}
